using UnityEngine;

public class DuckGrenade : MonoBehaviour
{
	public Rigidbody2D rigidBody;

	public Explosion explosion;

	public AudioSource audioSource;

	public AudioClip startAudioClips;

	public AudioClip[] bounceHardAudioClips;

	public AudioClip[] bounceSoftAudioClips;

	public float thrownHeight;

	private float lastBounceSoundTime;

	private Vector2 lastBounceSoundPos;

	private void Start()
	{
		base.gameObject.layer = base.transform.parent.gameObject.layer;
		audioSource.PlayOneShot(startAudioClips);
		base.transform.Rotate(0f, 0f, Random.Range(0f, 360f));
		base.transform.parent.transform.position -= Vector3.up * thrownHeight;
		base.transform.position += Vector3.up * thrownHeight;
	}

	private void FixedUpdate()
	{
		if (rigidBody.IsSleeping())
		{
			Explode();
		}
	}

	public void Explode()
	{
		Object.Instantiate(explosion, base.transform.position, Quaternion.identity).gameObject.SetActive(value: true);
		Object.Destroy(base.transform.parent.gameObject);
	}

	private void OnCollisionEnter2D(Collision2D collision)
	{
		PlayBounceSound();
	}

	private void PlayBounceSound()
	{
		float num = Vector2.Distance(lastBounceSoundPos, base.transform.position);
		if (!(num < 0.05f) && !(Time.time - lastBounceSoundTime <= 0.2f))
		{
			AudioClip clip = ((!(num > 0.5f)) ? bounceSoftAudioClips[Random.Range(0, bounceSoftAudioClips.Length)] : bounceHardAudioClips[Random.Range(0, bounceHardAudioClips.Length)]);
			lastBounceSoundTime = Time.time;
			lastBounceSoundPos = base.transform.position;
			audioSource.PlayOneShot(clip);
		}
	}
}
